package io.renren.modules.report.controller;


import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserRoleService;
import io.renren.modules.sys.service.SysUserService;
import io.renren.modules.sys.shiro.ShiroUtils;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.apache.commons.lang.ArrayUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author  lgl
 */
@Controller
@RequestMapping("/report/jasper")
public class JaspViewerController extends AbstractController {
	/**
	 * 所有用户列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		return R.ok().put("page", "page");
	}
	/**
	 * 返回iReport报表视图
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/report")
	public String report(Model model) {
		// 报表数据源
		//JRDataSource jrDataSource = new JRBeanCollectionDataSource(JavaBeanPerson.getList());
		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
		DataSource dataSource = (DataSource)webApplicationContext.getBean("dataSource");
		// 动态指定报表模板url
		model.addAttribute("url", "/WEB-INF/jasper/jasper.jasper");
		model.addAttribute("format", "pdf"); // 报表格式
		model.addAttribute("jrMainDataSource", dataSource);

		return "iReportView"; // 对应jasper-defs.xml中的bean id
	}

}

